package com.vanysoft.framework.base.action;

import com.vanysoft.business.sys.entity.Menu;
import com.vanysoft.business.sys.entity.Org;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类名称：BaseController
 * 类描述：基础Action
 * 创建人：SharpClaw_x
 * 创建时间：2017年5月19日 上午10:18:41
 * 修改人：SharpClaw_x
 * 修改时间：2017年5月19日 上午10:18:41
 * 版本信息：@version 1.0
 * Copyright (c) 2017SharpClaw-版权所有
 * 修改备注：
 *
 * @author Admin
 */
public class BaseController {

    private static ThreadLocal<ServletRequest> currentRequest = new ThreadLocal<ServletRequest>();
    private static ThreadLocal<ServletResponse> currentResponse = new ThreadLocal<ServletResponse>();
    @Resource
    private MessageSource messageSource;

    @ModelAttribute
    protected void initReqAndRep(HttpServletRequest request, HttpServletResponse response) {
        currentRequest.set(request);
        currentResponse.set(response);
    }

    protected HttpServletRequest getRequest() {
        return (HttpServletRequest) currentRequest.get();
    }

    protected HttpServletResponse getResponse() {
        return (HttpServletResponse) currentResponse.get();
    }


    /**
     * 获取国际化的消息
     *
     * @param code code
     * @param args 占位符
     * @return 返回结果
     */
    public String getMessage(String code, Object... args) {
        return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
    }

    /**
     * 获取国际化的消息
     *
     * @param code key
     * @return
     */
    protected String getMessage(String code) {
        return getMessage(code, (Object[]) null);
    }

    protected List<Menu> buildMenuTree(List<Menu> menuList) {
        List<Menu> menus = menuList.stream().filter(m -> StringUtils.isEmpty(m.getParentId())).sorted(Comparator.comparingInt(m -> m.getOrderNumber() == null ? 0 : m.getOrderNumber())).collect(Collectors.toList());
        menus.forEach(m -> buildMenuTree(m, menuList));
        return menus;
    }

    private void buildMenuTree(Menu menu, List<Menu> menuList) {
        List<Menu> menus = menuList.stream().filter(m -> Objects.equals(m.getParentId(), menu.getId())).sorted(Comparator.comparingInt(m -> m.getOrderNumber() == null ? 0 : m.getOrderNumber())).collect(Collectors.toList());
        menu.setChildren(menus);
        menus.forEach(m -> buildMenuTree(m, menuList));
    }

    public List<Org> buildOrgTree(List<Org> orgList) {
        List<Org> orgs = orgList.stream().filter(o -> StringUtils.isEmpty(o.getParentCode())).collect(Collectors.toList());
        orgs.forEach(o -> buildOrgTree(o, orgList));
        return orgs;
    }

    private void buildOrgTree(Org org, List<Org> orgList) {
        List<Org> orgs = orgList.stream().filter(o -> Objects.equals(o.getParentCode(), org.getCode())).collect(Collectors.toList());
        org.setChildren(orgs);
        orgList.forEach(o -> buildOrgTree(o, orgList));
    }

}
